import { useState, useRef } from 'react';
import { notification } from '../../Notification';

export const useFileUpload = (
  onImageUpload?: (imageUrl: string | null, file?: File) => void,
  onReferenceImageUpload?: (imageUrl: string | null) => void
) => {
  const [uploadedFile, setUploadedFile] = useState<File | null>(null);
  const [referenceFile, setReferenceFile] = useState<File | null>(null);
  const [imagePreview, setImagePreview] = useState<string | null>(null);
  const [referenceImagePreview, setReferenceImagePreview] = useState<string | null>(null);
  const fileInputRef = useRef<HTMLInputElement | null>(null);
  const referenceFileInputRef = useRef<HTMLInputElement | null>(null);

  // 文件上传处理
  const handleFileUpload = (event: React.ChangeEvent<HTMLInputElement>) => {
    const file = event.target.files?.[0];
    if (!file) return;

    // 检查文件格式
    const allowedTypes = ["image/png", "image/jpeg", "image/jpg"];
    if (!allowedTypes.includes(file.type)) {
      notification.warning("只支持 PNG 和 JPG 格式的图片");
      return;
    }

    // 检查文件大小 (限制为 10MB)
    const maxSize = 10 * 1024 * 1024; // 10MB
    if (file.size > maxSize) {
      notification.warning("文件大小不能超过 10MB");
      return;
    }

    setUploadedFile(file);

    // 创建图片预览
    const reader = new FileReader();
    reader.onload = (e) => {
      const result = e.target?.result as string;
      setImagePreview(result);
      // 通知父组件图片已上传
      onImageUpload?.(result, file);
    };
    reader.onerror = (e) => {
      console.error("FileReader 读取失败:", e);
    };
    reader.readAsDataURL(file);
  };

  // 参考图片上传处理
  const handleReferenceFileUpload = (event: React.ChangeEvent<HTMLInputElement>) => {
    const file = event.target.files?.[0];
    if (!file) return;

    // 检查文件格式
    const allowedTypes = ["image/png", "image/jpeg", "image/jpg"];
    if (!allowedTypes.includes(file.type)) {
      notification.warning("只支持 PNG 和 JPG 格式的图片");
      return;
    }

    // 检查文件大小 (限制为 10MB)
    const maxSize = 10 * 1024 * 1024; // 10MB
    if (file.size > maxSize) {
      notification.warning("文件大小不能超过 10MB");
      return;
    }

    setReferenceFile(file);

    // 创建图片预览
    const reader = new FileReader();
    reader.onload = (e) => {
      const result = e.target?.result as string;
      setReferenceImagePreview(result);
      // 通知父组件参考图已上传
      onReferenceImageUpload?.(result);
    };
    reader.onerror = (e) => {
      console.error("参考图片读取失败:", e);
    };
    reader.readAsDataURL(file);
  };

  // 触发文件选择
  const triggerFileUpload = () => {
    fileInputRef.current?.click();
  };

  // 触发参考图片文件选择
  const triggerReferenceFileUpload = () => {
    referenceFileInputRef.current?.click();
  };

  // 清除图片
  const clearImage = () => {
    setImagePreview(null);
    setUploadedFile(null);
    // 通知父组件图片已删除
    onImageUpload?.(null);
    if (fileInputRef.current) {
      fileInputRef.current.value = "";
    }
  };

  const clearReferenceImage = () => {
    setReferenceImagePreview(null);
    setReferenceFile(null);
    if (referenceFileInputRef.current) {
      referenceFileInputRef.current.value = "";
    }
  };

  // 设置图片URL（用于编辑模式）
  const setImageUrl = (imageUrl: string) => {
    setImagePreview(imageUrl);
    // 注意：这里不设置uploadedFile，因为编辑模式下我们只有URL
  };

  // 设置参考图URL（用于编辑模式）
  const setReferenceImageUrl = (imageUrl: string) => {
    setReferenceImagePreview(imageUrl);
    // 注意：这里不设置referenceFile，因为编辑模式下我们只有URL
  };

  return {
    uploadedFile,
    referenceFile,
    imagePreview,
    referenceImagePreview,
    fileInputRef,
    referenceFileInputRef,
    handleFileUpload,
    handleReferenceFileUpload,
    triggerFileUpload,
    triggerReferenceFileUpload,
    clearImage,
    clearReferenceImage,
    setImageUrl,
    setReferenceImageUrl
  };
};
